package com.ruoyi.business.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.protocol.IDynamicSearchService;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class EsTask {

    @Autowired
    private IDynamicSearchService dynamicSearchService;

    //每天凌晨1点执行
    @Scheduled(cron="0 0 1 * * ? ")
    public void delEs() {
        String index = "weipinhui_log";
        String offsetDate = DateUtils.getOffsetDate(-10);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(new RangeQueryBuilder("createTimeStr.keyword").lte(offsetDate).format("yyyy-MM-dd HH:mm:ss"));
        dynamicSearchService.deleteQueryDocument(index, boolQueryBuilder);
    }



    @Scheduled(cron="0 0 1 * * ? ")
    public void delRtaEs() {
        String offsetDate = DateUtils.getOffsetDate(-1);
        offsetDate = offsetDate.substring(0, 10).replaceAll("-", "");
        //youdao_log_rta_20240913
        dynamicSearchService.deleteIndex("youdao_log_rta_"+offsetDate);
    }

//    @PostConstruct
//    public void delRTA() {
//        String index = "weipinhui_log";
//        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//        boolQueryBuilder.must(QueryBuilders.termQuery("businessTypeStr.keyword", "RTA"));
//        dynamicSearchService.deleteQueryDocument(index, boolQueryBuilder);
//    }

}
